package com.ch.dream.logic.shortstory;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.ch.dream.R;
import com.ch.dream.activity.shortstory.CollectActivity;
import com.ch.dream.activity.shortstory.StoryContentActivity;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.database.MyDatabase;
import com.ch.dream.tools.ViewTools;

public class CollectLogic extends BaseLogic {

	public CollectLogic(CollectActivity activity) {
		super(activity);
	}
	public CollectLogic(CollectActivity activity,int layoutId) {
		super(activity,layoutId);
	}
	
	private ListView lvCollect;
	private SimpleCursorAdapter caCollect;
	private MyDatabase myDatabase;
	private Cursor myCursor;
	private void initViews(){
		lvCollect = (ListView)activity.findViewById(R.id.lvCollect);
	}
	
	public void init(){
		initViews();
		initCollectList();
	}
	
	private void initCollectList(){
		myDatabase =null;
		myCursor = null;
		try{
			myDatabase = new MyDatabase(activity);
			String[] strArray = new String[]{"_id","content","createTime"};
			String[] collectArray = new String[]{"content","createTime"};
			myCursor = myDatabase.query(MyDatabase.shortStoryCollect,strArray
			, null, null, " _id desc");
			caCollect = new SimpleCursorAdapter (activity,R.layout.s_collect_item,myCursor,collectArray,new int[]{R.id.tvContent,R.id.tvCreateTime}){
				@Override
				public View getView(int position, View convertView,
						ViewGroup parent) {
					return super.getView(position, convertView, parent);
				}
			};
			lvCollect.setAdapter(caCollect);
			lvCollect.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> data, View view,
						int position, long id) {
					Cursor select = (Cursor)data.getAdapter().getItem(position);
					Intent intent = new Intent(activity,StoryContentActivity.class);
					intent.putExtra("content", select.getString(select.getColumnIndex("content")));
					activity.startActivity(intent);
				}
			});
			lvCollect.setOnItemLongClickListener(new OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(final AdapterView<?> data, View view,
						final int position, long id) {
					ViewTools.showConfirm(activity, "提示信息", "确认要删除吗？", "确认","取消", new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Cursor select = (Cursor)data.getAdapter().getItem(position);
							myDatabase = new MyDatabase(activity);
							myDatabase.delete(MyDatabase.shortStoryCollect, "_id = "+select.getString(select.getColumnIndex("_id")), null);
							String[] strArray = new String[]{"_id","content","createTime"};
							myCursor = myDatabase.query(MyDatabase.shortStoryCollect,strArray, null, null, " _id desc");
							if(myCursor.getCount()==0){
								activity.finish();
								return;
							}
							caCollect.changeCursor(myCursor);
							caCollect.notifyDataSetChanged();
						}
					},new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
					
					return true;
				}
			});
		}catch (Exception e) {
			e.printStackTrace();
			ViewTools.showShortToast(activity, "数据出错...");
		}finally{
			if(myDatabase!=null){
				myDatabase.close();
			}
		}
	}
	
	public void clearCollect(){
		myDatabase = new MyDatabase(activity);
		myDatabase.delete(MyDatabase.shortStoryCollect, null, null);
		String[] strArray = new String[]{"_id","content","createTime"};
		myCursor = myDatabase.query(MyDatabase.shortStoryCollect,strArray, null, null, " _id desc");
		if(myCursor.getCount()==0){
			activity.finish();
			return;
		}
		caCollect.changeCursor(myCursor);
		caCollect.notifyDataSetChanged();
		
	}
}
